$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/songsetnew/list',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden: true},
            { label: '曲集名称', name: 'name', index: 'name', width: 80 },
            { label: '曲集难度', name: 'hardName', index: 'hard', width: 80 },
            { label: '排序', name: 'sort', index: 'sort', width: 80 },
            { label: '上传者', name: 'uploadUser', index: 'upload_user', width: 80 },
            { label: '上架时间', name: 'releaseTime', index: 'release_time', width: 80 }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        songsetNew: {},
        songList: []
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.songsetNew = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.songsetNew.id == null ? "sys/songsetnew/save" : "sys/songsetnew/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.songsetNew),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/songsetnew/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function(id){
            $.get(baseURL + "sys/songsetnew/info/"+id, function(r){
                vm.songsetNew = r.songsetNew;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
        },
        checkSong:function () {
            $.ajax({
                type: "POST",
                url: baseURL + "sys/songsetnew/listAll",
                contentType: "application/json",
                data: JSON.stringify({}),
                success: function(r){
                    if(r.code == 0){
                        vm.songList = r.list;
                    }else{
                        alert(r.msg);
                    }
                }
            });
            var layerIndex=layer.open({
                type: 1 ,//Page层类型
                area: ['1000px', '800px'],
                title: '曲集列表',
                shade: 0.6, //遮罩透明度,
                maxmin: false, //允许全屏最小化,
                anim: 1, //0-6的动画形式，-1不开启,
                btn: ['提交'],
                yes:function (index) {
                    var ids = $("#jqGridSong").jqGrid('getGridParam','selarrrow');
                    var sortList=[];
                    $.each(ids,function(index,item){
                        $('#jqGridSong').saveRow(item, {url : "clientArray"});
                        sortList.push({songsetId:item,sort:$("#"+item+"_sort").val()});
                    });
                    //排序提交
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/songsetnew/checkSong",
                        contentType: "application/json",
                        data: JSON.stringify(sortList),
                        success: function(r){
                            if(r.code == 0){
                                alert('操作成功', function(index){
                                    vm.reload();
                                    layer.closeAll();
                                });
                            }else{
                                alert(r.msg);
                            }
                        }
                    });
                },
                content: '<div style="padding:20px;">' +
                '<div class="grid-btn">' +
                '<div class="form-group col-sm-2">\n' +
                '<input type="text" class="form-control" id="songNameId" @keyup.enter="searchSong" placeholder="曲集名称">\n' +
                '</div>' +
                '<a class="btn btn-default" onclick="vm.searchSong();">搜索</a>' +
                '</div>' +
                '<table id="jqGridSong"></table>' +
                '<div id="jqGridPagerSong"></div>' +
                '</div>'
            });
            layer.full(layerIndex);
            $("#jqGridSong").jqGrid({
                url: baseURL + 'sys/songsetnew/recommendList',
                datatype: "json",
                colModel: [
                    { label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden: true},
                    { label: '曲集名称', name: 'name', index: 'name', width: 80 },
                    { label: '曲集难度', name: 'hardName', index: 'hard', width: 80 },
                    { label: '上架时间', name: 'releaseTime', index: 'release_time', width: 80 },
                    { label: '排序', name: 'sort', index: 'sort', width: 80 ,editable: true, edittype: "number"}
                ],
                viewrecords: true,
                height: 385,
                rowNum: 20,
                rowList : [20,50,100],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                pager: "#jqGridPagerSong",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    $("#jqGridSong").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                },
                loadComplete:function(){
                    if (vm.songList.length > 0) {
                        $.each(vm.songList, function (i, item) {
                            $("#jqGridSong").jqGrid('setSelection',item.id);
                        });
                    }
                },
                onSelectRow:function (id) {
                    $("#jqGridSong").jqGrid('editRow', id, true);
                }
            });
        },
        searchSong:function () {
            var page = $("#jqGridSong").jqGrid('getGridParam','page');
            $("#jqGridSong").jqGrid('setGridParam',{
                postData:{
                    name:$("#songNameId").val()
                },
                page:page
            }).trigger("reloadGrid");
        }
    }
});